// pages/admin/department/departDetail/departDetail.
import Api from '../../../../utils/http'
var wxCharts = require('../../../../utils/wxcharts.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weekLaunch: 2, //为1时是周投放量加粗字体，0为正常
    monthLaunch: 1, //为2时是月投放量加粗字体，0为正常
    recovery: '', //回收
    columnDetails: [], //分类数据
    lineDetails: [], //周、月数据
    canvasList: [],
    // current: '', //当前信息
    // charge:'',//部门负责人
    input: null, //输入数量
    searchFlag: true,
    openCode:null,//部门关闭判断
    profit:'',
    state:null,//部门状态值
  },
  //关闭部门
  closeDepart: function (e) {
    // console.log(this.data,e);
    this.getClose(e);
  },
  //可发放袋子数量
  grant: function () {
    //点击输入框
    // console.log(this.data)
    if (this.data.state==2) {
      wx.showToast({
        title: '该部门已关闭比',
        icon:'none',
        duration:1000
      })
    }else{
      this.setData({
        searchFlag: false
      })
    }
  },
  //获取输入数量
  input: function (e) {
    var that = this;
    that.setData({
      input: e.detail.value
    });
    if (that.data.input > 0) {
      wx.showModal({
        title: '提示',
        content: '请核对发放数量，然后点击确定',
        success (res) {
          if (res.confirm) {
            that.getsendBag();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else if (that.data.input <= 0) {
      wx.showToast({
        title: '可发放袋子数最少为1',
        icon:'none',
        duration:1000
      })
    }
    
    
  },
  //周投放量、
  weekLaunch: function (e) {
    var that = this;
    console.log(_index)
    let _index = e.currentTarget.dataset.index
    var weekLaunch = that.data.weekLaunch;
    if (weekLaunch == 2) {
      weekLaunch = 0
    } else {
      weekLaunch = 2
    }
    that.setData({
      weekLaunch: weekLaunch
    })
    that.getweekData();
  },
  //月投放量
  monthLaunch: function (e) {
    var that = this;
    console.log(that.data)
    var monthLaunch = that.data.monthLaunch;
    if (monthLaunch == 1) {
      monthLaunch = 0
    } else {
      monthLaunch = 1
    }
    that.setData({
      monthLaunch: monthLaunch
    })

    that.getMonthData();
  },
  //更换、绑定负责人
  replace: function () {
    var that = this;
    console.log(that.data);
    var time = that.data.recovery.create_time;
    var did = that.data.recovery.id;
    var name = that.data.recovery.title
    wx.navigateTo({
      url: `/pages/admin/department/departAddStepTwo/departAddStepTwo?time=${time}&did=${did}&name=${name}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    });
    var departName = options.departName;
    wx.setNavigationBarTitle({
      title: departName
    });
    var weekLaunch = that.data.weekLaunch;
    var monthLaunch = that.data.monthLaunch;
    if (weekLaunch == 2) {

    } else if (monthLaunch == 1) {

    }
    that.getDepartment();
    that.getColumnData();
    that.getweekData();
    that.getIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
//首页数据
  getIndex: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type
    }
    console.log(param)
    Api.ajax("/app/Zindex/index", param).then(res => {
      console.log('getIndex');
      console.log(res)
      if (res.code == 200) {
        that.setData({
          profit: res.data,
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //关闭部门
  getClose:function(e){
    var that=this;
    var uid=wx.getStorageSync('userid');
    var param={
      uid:uid.id,
      did: parseInt(that.data.options.lid),
      type:e.currentTarget.dataset.type
    }
    console.log(param)
    Api.ajax('/app/Zmanage/close',param).then(res=>{
      console.log(res)
      if(res.code==200){
        wx.showToast({
          title: '部门关闭成功',
          duration:1000
        });
        that.setData({
          openCode:res.code
        })
      }else if(res.code==202){
        wx.showToast({
          title: '关闭失败',
          icon:'none',
          duration:1000
        });
      }
    })
  },
  //月折线图
  lineCanvas: function () {
    var that = this;
    // 历史数据
    let lineDetails = that.data.lineDetails;
    var month = [];
    var datas = [];
    console.log(lineDetails)

    for (var i in lineDetails) {

      var obj = {
        month: lineDetails[i].month,
        data: lineDetails[i].weight
      }
      month = month.concat(obj.month);
      //console.log(month);
      datas = datas.concat(obj.data);
      // console.log(datas);
    }
    that.setData({
      month: month,
      datas: datas,
    });
    // 历史数据视图
    new wxCharts({
      canvasId: 'lineWeek',
      type: 'line',
      categories: month,
      animation: true,
      background: '#ffffff',
      series: [{
        name: '3月',
        data: datas,
        color: '#00c188',
        format: function (val, name) {
          return val + 'kg';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        min: 0,
      },
      width: 360,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      legend: false,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //周折线图
  weekCanvas: function () {
    var that = this;
    // 历史数据
    let lineDetails = that.data.lineDetails;
    var datas = [];
    console.log(lineDetails)
    for (var i in lineDetails) {
      var obj = {
        data: lineDetails[i].weight
      }
      datas = datas.concat(obj.data);
      // console.log(datas);
    }
    that.setData({
      datas: datas,
    });
    // 历史数据视图
    new wxCharts({
      canvasId: 'lineWeek',
      type: 'line',
      categories: ['第一周', '第二周', '第三周', '第四周', '第五周'],
      animation: true,
      background: '#ffffff',
      series: [{
        name: '3月',
        data: datas,
        color: '#00c188',
        format: function (val, name) {
          return val + 'kg';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        min: 0,
      },
      width: 360,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      legend: false,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //柱状条形图
  stripCanvas: function () {
    //分类
    var that = this;
    let columnDetails = that.data.columnDetails;
    var title = [];
    var weights = [];
    for (var i in columnDetails) {
      var obj = {
        title: columnDetails[i].title,
        weights: columnDetails[i].weight
      }
      title = title.concat(obj.title);
      //console.log(title);
      weights = weights.concat(obj.weights);
      // console.log(weights);
    }
    that.setData({
      title: title,
      weights: weights,
    });
    //分类信息
    new wxCharts({
      canvasId: 'lineCanvas',
      type: 'column',
      categories: title,
      series: [{
        names: '综合纸',
        data: weights,
        format: function (val, name) {
          return val + 'kg';
        }
      }],
      yAxis: {
        min: 0,
      },
      extra: {
        column: {
          width: 25
        }
      },
      width: 350,
      height: 300,
      dataLabel: true,
      legend: false,
    });
  },
  //投放周走势数据
  getweekData: function () {
    var that = this;
    var param = {
      type: 2,
      selType: 2,
      did: parseInt(that.data.options.lid),
    };
    console.log(param)
    Api.ajax("/app/Zmanage/launchData", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          lineDetails: res.data,
        })
        that.weekCanvas();

      }
    });
  },
  //投放月走势数据
  getMonthData: function () {
    var that = this;
    var param = {
      type: 1,
      selType: 2,
      did: that.data.options.lid,
    };
    console.log(param)
    Api.ajax("/app/Zmanage/launchData", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          lineDetails: res.data,
        })
        that.lineCanvas();

      }
    });
  },
  //投放分类走势
  getColumnData: function () {
    var that = this;
    var param = {
      type: 1,
      did: that.data.options.lid,
      selType: 2
    };
    console.log(param)
    Api.ajax("/app/Zmanage/dataForm", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          columnDetails: res.data,
        })
        that.stripCanvas();

      }
    });
  },
  //部门详情数据
  getDepartment: function (options) {
    var that = this;
    var param = {
      did: parseInt(that.data.options.lid),
    }
    console.log(param)
    Api.ajax('/app/Zmanage/departmentDetail', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          recovery: res.data,
          state:res.data.status
        })
      }
    })
  },
  // //当前
  // current: function () {
  //   var that = this;
  //   let uid = wx.getStorageSync('userid')
  //   var param = {
  //     uid: uid.id,
  //     user_type: uid.user_type,
  //   }
  //   Api.ajax('/app/Zindex/index', param).then(res => {
  //     console.log(res)
  //     if (res.code == 200) {
  //       that.setData({
  //         current: res.data
  //       })
  //     }
  //   })
  // },
  //单位至部门发放袋子
  getsendBag: function () {
    var that = this;
    var uid = wx.getStorageSync('userid');
    var param = {
      uid: parseInt(uid.id),
      did: parseInt(that.data.options.lid),
      num: parseInt(that.data.input)
    }
    console.log(param)
    Api.ajax('/app/Zmanage/sendBag', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.showToast({
          title: '发放成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '可发袋子数不足',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})